package com.bangbang.controller;


import com.bangbang.configresult.Result;
import com.bangbang.configresult.ResultEnum;
import com.bangbang.mapper.TbAdminMapper;
import com.bangbang.pojo.TbAdmin;
import com.bangbang.pojo.TbTeacher;
import com.bangbang.util.IdWorker;
import com.bangbang.util.ShiroUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.UUID;

/**
 * <p>
 * 运营商表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-08-13
 */
@RestController
@RequestMapping("tbAdmin")
public class TbAdminController {

    @Autowired
    private TbAdminMapper tbAdminMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    // 教师端注册
    @ApiOperation(value = "运营商注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbAdmin",value = "运营商表的实体类",required = true,paramType = "body"),
    })
    @PostMapping
    public Result studentRegister (@RequestBody TbAdmin tbAdmin){
        if ("".equals(tbAdmin.getAdminPwd())){
            return Result.buildResult(ResultEnum.ERROR).setResult("不能为空");
        }
        String salt = ShiroUtils.generateSalt(6);
        String password = tbAdmin.getAdminPwd();
        tbAdmin.setAdminPwd(ShiroUtils.encryptPassword("MD5", password, salt, 3));
        IdWorker idWorker = new IdWorker();
        long teacherAccount = idWorker.nextId();
        String account = String.valueOf(teacherAccount);
        tbAdmin.setAdminName(account).setAdminSalt(salt);
        tbAdminMapper.insert(tbAdmin);
        return Result.buildResult(ResultEnum.SUCCESS);
    }


    //    教师登录
    @ApiOperation(value = "运营商登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "AdminAccount",value = "运营商账号",required = true,paramType = "query"),
            @ApiImplicitParam(name = "AdminPwd",value = "运营商密码",required = true,paramType = "query"),
    })
    @GetMapping
    public Result Login(@RequestParam(required = true)String AdminAccount,@RequestParam(required = true) String AdminPwd){
        Subject subject = SecurityUtils.getSubject();
        String user = "3";
        UsernamePasswordToken token = new UsernamePasswordToken(AdminAccount,AdminPwd,user);
        //        account  获取到登录用户的对象
        QueryWrapper<TbAdmin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("admin_name", AdminAccount);
        TbAdmin tbAdmin = tbAdminMapper.selectOne(adminQueryWrapper);
        String NowUser= UUID.randomUUID().toString();
        NowUser.replace("-","");
        redisTemplate.boundValueOps(NowUser).set(tbAdmin.getAdminId());
        return Result.buildResult(ResultEnum.SUCCESS).setResult(NowUser);//把这个uuid给前台,拿去判断
    }


}

